package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.WisdomTrailsEquipmentMapper;
import com.dwxt.sport.business.dao.WisdomTrailsMapper;
import com.dwxt.sport.business.entity.WisdomTrails;
import com.dwxt.sport.business.entity.WisdomTrailsEquipment;
import com.dwxt.sport.business.vo.wisdomTrails.InsertOrUpdateWisdomTrailsEquipmentVo;
import com.dwxt.sport.business.vo.wisdomTrails.InsertOrUpdateWisdomTrailsVo;
import com.dwxt.sport.business.vo.wisdomTrails.SelectWisdomTrailsVo;
import com.dwxt.sport.constant.CommonConstant;
import com.dwxt.sport.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 智慧步道
 *
 * @author Kuang.JiaZhuo
 * @date 2020-07-31 15:23
 */
@Service
public class WisdomTrailsService {

    @Resource
    WisdomTrailsMapper wisdomTrailsMapper;

    @Resource
    WisdomTrailsEquipmentMapper equipmentMapper;

    /**
     * @方法名：insertOrUpdateWisdomTrails
     * @描述： 新增或修改智慧步道
     * @作者： kjz
     * @日期： Created in 2020/7/31 16:05
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateWisdomTrails(InsertOrUpdateWisdomTrailsVo insertOrUpdateVo) {
        String id = insertOrUpdateVo.getId();

        WisdomTrails wisdomTrails = new WisdomTrails()
                .setName(insertOrUpdateVo.getName()) //设备名称
                .setRecommendedCoverage(insertOrUpdateVo.getRecommendedCoverage()) //建议覆盖率
                .setUnitType(insertOrUpdateVo.getUnitType()) //设备型号
                .setEquipmentsImgUrl(insertOrUpdateVo.getEquipmentsImgUrl()); //设备图片地址

        if(null==id || id.trim().length()==0){
            //新增
            String streetId = insertOrUpdateVo.getStreetId();
            if(null==streetId || streetId.trim().length()==0){
                throw new CustomException(StatusCode.STREET_ID_IS_NULL);
            }

            wisdomTrails.setId(SnowIdUtil.getInstance().nextId())
                    .setAmount(0) //新增时设备数量为1
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setStreetId(streetId); //街道id
            return wisdomTrailsMapper.insert(wisdomTrails);
        }else{
            //更新
            wisdomTrails.setUpdateTime(LocalDateTime.now()).setId(id);
            return wisdomTrailsMapper.updateByPrimaryKeySelective(wisdomTrails);
        }
    }

    /**
     * @方法名：selectWisdomTrailsByCriteria
     * @描述： 按条件查询智慧步道列表(带分页)
     * @作者： Kjz
     * @日期： Created in 2020/7/31 16:32
     */
    public PageInfo<WisdomTrails> selectWisdomTrailsByCriteria(SelectWisdomTrailsVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        List<WisdomTrails> list = wisdomTrailsMapper.selectWisdomTrailsByCriteria(selectVo);
        list.forEach(wisdomTrails -> {
            wisdomTrails.setEquipmentsImgUrl(CommonConstant.FILE_SERVER+wisdomTrails.getEquipmentsImgUrl());
        });
        PageInfo<WisdomTrails> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteWisdomTrailsByIds
     * @描述： 根据ids批量删除智慧步道
     * @作者： kjz
     * @日期： Created in 2020/7/31 16:35
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteWisdomTrailsByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.wisdomTrailsMapper.deleteWisdomTrailsByIds(idArray);
    }

    /**
     * @方法名：insertOrUpdateWisdomTrailsEquipment
     * @描述： 新增 或 更新【智慧步道】设备关联
     * @作者： kjz
     * @日期： Created in 2020/7/31 16:39
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateWisdomTrailsEquipment(InsertOrUpdateWisdomTrailsEquipmentVo insertOrUpdateVo) {

        String id = insertOrUpdateVo.getId();
        WisdomTrailsEquipment wisdomTrailsEquipment = new WisdomTrailsEquipment()
                .setEquipmentId(insertOrUpdateVo.getEquipmentId()) //设备id
                .setTodayPeople(insertOrUpdateVo.getTodayPeople()) //今日人次
                .setPeopleAlways(insertOrUpdateVo.getPeopleAlways()) //总人次
                .setDeviceStates(insertOrUpdateVo.getDeviceStates()); //设备状态

        String wisdomTrailsId = insertOrUpdateVo.getWisdomTrailsId();
        int result = 0;
        if (null == id || id.trim().length() == 0) {
            //新增

            if (null == wisdomTrailsId || wisdomTrailsId.trim().length() == 0) {
                throw new CustomException(StatusCode.WISDOM_TRAILS_ID_IS_NULL);
            }
            wisdomTrailsEquipment.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setWisdomTrailsId(wisdomTrailsId); //体育培训关联id

            result = equipmentMapper.insert(wisdomTrailsEquipment);

        } else {
            //更新
            wisdomTrailsEquipment.setUpdateTime(LocalDateTime.now()).setId(id);
            result =equipmentMapper.updateByPrimaryKeySelective(wisdomTrailsEquipment);
        }

        if(result>0){
            this.updateWisdomTrails(wisdomTrailsId);
        }

        return result;
    }

    /**
     * @方法名：updateWisdomTrails
     * @描述： 新增、删除、更新智慧步道之后 再对智慧步道需要计算的数据做更新
     * @作者： kjz
     * @日期： Created in 2020/8/5 14:32
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateWisdomTrails(String wisdomTrailsId) {
        HashMap map = equipmentMapper.selectStatisticsByWisdomTrailsId(wisdomTrailsId);

        WisdomTrails wisdomTrails = new WisdomTrails()
                .setAmount(Integer.valueOf(map.get("amount").toString()))
                .setUpdateTime(LocalDateTime.now())
                .setId(wisdomTrailsId);

        return wisdomTrailsMapper.updateByPrimaryKeySelective(wisdomTrails);
    }

    /**
     * @方法名：deleteWisdomTrailsEquipmentByIds
     * @描述： 根据ids批量删除【智慧步道】设备关联
     * @作者： kjz
     * @日期： Created in 2020/7/31 16:55
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteWisdomTrailsEquipmentByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        int result = this.equipmentMapper.deleteWisdomTrailsEquipmentByIds(idArray);
        if(result>0){
            String id = idArray[0];
            String wisdomTrailsId = this.equipmentMapper.selectByPrimaryKey(id).getWisdomTrailsId();
            this.updateWisdomTrails(wisdomTrailsId);
        }

        return result;
    }

    /**
     * @方法名：selectWisdomTrailsEquipmentByPage
     * @描述： 根据【智慧步道】的id查询关联设备的列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/7/31 17:00
     */
    public PageInfo<WisdomTrailsEquipment> selectWisdomTrailsEquipmentByPage(Integer pageNum, Integer pageSize, String wisdomTrailsId) {
        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
        List<WisdomTrailsEquipment> list = equipmentMapper.selectWisdomTrailsEquipmentByPage(wisdomTrailsId);
        PageInfo<WisdomTrailsEquipment> pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
